const _ = require('lodash');
var mongoose = require('mongoose');
var FoodModel = mongoose.model('Food');

module.exports = {
    findFoodListByIcnum: findFoodListByIcnum,
    getFoodByIcnumFtype: getFoodByIcnumFtype,
    addFood:    addFood
}


function findFoodListByIcnum(req, res, next) {
    let icnum = req.params.icnum
    if (_.isEmpty(icnum)) {
        return next(new Error(err));
    } else {
        FoodModel.find({'merchantIcnum':icnum},function(err, doc){
            if (err) {
                return next(new Error(err));
            } else {
                return res.json(doc);
            }  
            
        })
    }
}

function getFoodByIcnumFtype(req, res, next){
    let icnum = req.params.icnum;
    let ftype = req.params.ftype;
    FoodModel.find({
        merchantIcnum : icnum,
        ftype : ftype
        },function(error,data){
            if (error) {
                return next(new Error(error));
            } else {
                return res.json(data);
            }
        });
}

function addFood(req, res, next){
    let food = req.body;
    FoodModel.create(food,function(error,data){
        if (error) {
            return next(new Error(error));
        } else {
            return res.json(data);
        }
    });
}

